package challenge;

import java.util.HashMap;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by Tzachi on 18/10/2014.
 */
public class Tango {

    String temp[] = {"a", "b"};

    public static void add(int a) {
        loop:
        for (int i = 1; i < 3; i++) {
            for (int j = 1; j < 3; j++) {
                if (a == 5) {
                    break loop;
                }
                System.out.println(i * j);
            }

        }
    }

    /**
     * Add one item of the product with ID productdId to the shopping cart for the user identified by sessionId
     * shoppingCarts contains all shopping carts for all users
     * a shopping cart for a user is represented as a Vector<Long>
     * - each Long represents an individual item in a cart
     * - duplicates are ok and indicate that multiple items of the same product are in the cart
     * <p>
     * this method must succeed
     * exit criteria is that you have added the product to the shopping cart associated with the user in a thread safe manner
     */
    public void addElement(String sessionId, Long productId, ConcurrentHashMap<String, Vector<Long>> shoppingCarts) {
        // in case during this function call someone deletes the vector associated with the sessionId
        Vector<Long> ifDeletedVector = new Vector<Long>();
        ifDeletedVector.add(productId);

        // now I need to figure out how to make sure there is always an insertion
        // if putIfAbsent null -> there was no mapping to entry -> we entered a new vector with one product -> done
        // if putIfAbsent !null -> there was a previous entry -> results holds it now -> we need to update it
        Vector<Long> result;
        if ((result = shoppingCarts.putIfAbsent(sessionId, ifDeletedVector)) != null) {
            // results is cannot be null (otherwise we would never reach here)
            // we can be sure results hold the updated reference to the vector (until here)
            // thread safe due to vector
            result.add(productId);

            // we know that an update to an existing key was needed before
            // but before, but now we need to check again if deletion happened right after the previous if

            // we will try to replace the previous vector with our new one
            // if replace !null -> there were no deletion in between -> we successfully updated the value -> done
            //      (technically, in this case replace did not do anything since we added to product to the cart on the previous line
            //       but we want to make sure that no one deleted it)
            // if replace == null -> the value was deleted between the previous call and here -> we need to put the ifDeletedVector -> done
            if (shoppingCarts.replace(sessionId, result) == null) {
                shoppingCarts.putIfAbsent(sessionId, ifDeletedVector);
            }
        }
    }

    public void removeCart(String sessionId, ConcurrentHashMap<String, Vector<Long>> shoppingCarts) {
        shoppingCarts.remove(sessionId);
    }


    public static void main(String[] args) {
        Long foo = new HashMap<String, Long>().put("a", 1L);
        System.out.println(foo);
        add(5);

        Vector v = new Vector();
    }
}
